from django.db import models
from .models import User

class Address(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='addresses')
    receiver = models.CharField(max_length=50)
    phone = models.CharField(max_length=15)
    province_code = models.CharField(max_length=50)
    city_code = models.CharField(max_length=50)
    county_code = models.CharField(max_length=50)
    detail = models.TextField()
    postal_code = models.CharField(max_length=6, blank=True, null=True)
    tags = models.CharField(max_length=100, blank=True, null=True)
    is_default = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['-is_default', '-created_at']

    def __str__(self):
        return f"{self.receiver}'s address at {self.province_code} {self.city_code}"